package com.yunshipei.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;

import com.yunshipei.base.BaseActivity;
import com.yunshipei.common.wedigt.YspTitleBar;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.DateString;
import com.yunshipei.model.ForwardTimeModel;
import com.yunshipei.model.NotifyInfo;
import com.yunshipei.ui.fragment.DelayFragment;
import com.yunshipei.ui.fragment.DestoryNotifiListFragment;
import com.yunshipei.ui.fragment.ForwardFragment;
import com.yunshipei.ui.fragment.NotifiContextEditFragment;
import com.yunshipei.ui.fragment.RepetitionFragment;
import com.yunshipei.ui.fragment.TimeFragment;

//import de.greenrobot.event.EventBus;
import io.rong.eventbus.EventBus;
public class AlarmSetActivity extends BaseActivity implements View.OnClickListener {
    public static final int FORWARDTIME = 1;
    public static final int DATETIME = 2;
    public static final int REPETITIONTIME = 3;
    private Intent intent = null;
    private Uri uri = null;
    private OnInfomationSaveListener onInfomationSaveListener = null;
    private NotifyInfo notifiInfo = null;
    private Fragment fragment;
    private String tag = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_set);
        initView();
    }

    public void initView() {
        initTitleBar();
        fragment = null;
        intent = getIntent();
        uri = intent.getData();
        notifiInfo = (NotifyInfo) intent.getSerializableExtra("notifi");
        setTitle(uri);
        if (fragment != null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.de_content, fragment, tag);
            transaction.addToBackStack(null).commitAllowingStateLoss();
        }
    }

    private YspTitleBar mTitleBar;

    private void initTitleBar() {
        mTitleBar = (YspTitleBar) findViewById(R.id.title_bar);
        mTitleBar.setSubTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInfomationSaveListener.onInfomationSave();
            }
        });
    }

    private void setTitle(Uri uri) {
        if (uri != null) {
            if (uri.getScheme().equals("Enterplorer") && uri.getPathSegments().get(0).equals("DateTime")) {
                mTitleBar.setTitle("日期设置");
                tag = uri.getQueryParameter("target");
                //时间选择fragment
                fragment = new TimeFragment();
            } else if (uri.getScheme().equals("Enterplorer") && uri.getPathSegments().get(0).equals("ForwardTime")) {
                mTitleBar.setTitle("提前提醒设置");
                tag = uri.getQueryParameter("target");
                fragment = new ForwardFragment();
            } else if (uri.getScheme().equals("Enterplorer") && uri.getPathSegments().get(0).equals("RepetitionTime")) {
                mTitleBar.setTitle("提醒重复设置");
                fragment = new RepetitionFragment();
            } else if (uri.getScheme().equals("Enterplorer") && uri.getPathSegments().get(0).equals("DelaySet")) {
                mTitleBar.setTitle("提醒延迟设置");
                fragment = new DelayFragment();
            } else if (uri.getScheme().equals("Enterplorer") && uri.getPathSegments().get(0).equals("Destory")) {
                mTitleBar.setTitle("历史记录");
                fragment = new DestoryNotifiListFragment();
            } else if (uri.getScheme().equals("Enterplorer") && uri.getPathSegments().get(0).equals("Context")) {
                mTitleBar.setTitle("提醒内容设置");
                fragment = new NotifiContextEditFragment();
            }
        }
    }

    private void initData() {

    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
            super.onBackPressed();
            this.finish();
        } else {
            super.onBackPressed();
        }

    }

    @Override
    public void onClick(View v) {

    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case FORWARDTIME:
                    Bundle bundle = msg.getData();
                    Log.e("FORWARTIME", bundle.getString("time") + "    ******  " + bundle.getString("timeType"));
                    ForwardTimeModel forwardTimeModel = new ForwardTimeModel(Integer.parseInt(bundle.getString("time")), bundle.getString("timeType"));
                    EventBus.getDefault().post(forwardTimeModel);
                    break;
                case DATETIME:
                    String dateStr = msg.getData().getString("year") + "-" + msg.getData().getString("month") + "-" + msg.getData().getString("day");
                    Log.e("DATETIME", dateStr);
                    DateString ds = new DateString();
                    ds.setDateString(dateStr);
                    EventBus.getDefault().post(ds);
                    break;
                case REPETITIONTIME:

                    break;
            }
        }
    };

    public interface OnInfomationSaveListener {
        void onInfomationSave();
    }

    public void setOnInfomationSaveListener(OnInfomationSaveListener onInfomationSaveListener) {
        this.onInfomationSaveListener = onInfomationSaveListener;
    }

    public NotifyInfo getNotifiInfo() {
        return notifiInfo;
    }

    public void setNotifiInfo(NotifyInfo notifiInfo) {
        this.notifiInfo = notifiInfo;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}